import React, { createContext, useState, useContext, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as SplashScreen from 'expo-splash-screen';

const Context = createContext();
// consummer消费者，以后可以使用context内容
export const useCollect = () => {
    return useContext(Context);
}
// Provider,提供者，提供容器
const Provider = (props) => {
    const [isReady, setisReady] = useState(false);
    const [isCollect, setIsCollect] = useState([false, false, false, false, false, false]);

    useEffect(() => {
        SplashScreen.preventAutoHideAsync();

        AsyncStorage.getItem('install')
            .then(res => {
                if (res) {
                    console.log(res);
                    setIsCollect(JSON.parse(res));
                }
                setisReady(true);
                SplashScreen.hideAsync();
            })
    }, [])
    if (!isReady) {
        return null;
    }
    return (
        <Context.Provider value={{ isCollect, setIsCollect }}>
            {props.children}
        </Context.Provider>
    )
}
export default Provider;
